"""
created@2019/11/2
"""
from .base import _BaseModel
from extensions import db


class BaseModel(_BaseModel):
    __abstract__ = True
    __table_args__ = {
        'schema': 'silo'
    }


class Message(BaseModel):
    """微信推送消息"""

    __tablename__ = 'messages'

    title = db.Column(db.String(20), nullable=False, default='')
    wx_app_id = db.Column(db.Integer, nullable=False, default=0)
    deleted_at = db.Column(db.TIMESTAMP, default=None)
    msg_type = db.Column(db.String(10), nullable=False, default='text')
    content = db.Column(db.Text, nullable=False, default='')
    media_id = db.Column(db.String(100), nullable=False, default='')


class Group(BaseModel):
    """微信公众号推送对象"""

    __tablename__ = 'groups'

    wx_app_id = db.Column(db.Integer, nullable=False, default=0)
    parent_id = db.Column(db.Integer, nullable=False, default=0)
    title = db.Column(db.String(30), nullable=False, default='')
    description = db.Column(db.String(100), nullable=False, default='')
    query_sql = db.Column(db.Text, nullable=False, default='')
    deleted_at = db.Column(db.TIMESTAMP, default=None)
    total_count = db.Column(db.Integer, nullable=False, default=0)


class GroupMessage(BaseModel):
    """公众号指定人群推送消息"""

    __tablename__ = 'group_messages'

    wx_app_id = db.Column(db.Integer, nullable=False, default=0)
    msg_id = db.Column(db.Integer, nullable=False, default=0)
    group_id = db.Column(db.Integer, nullable=False, default=0)


class GroupUser(BaseModel):
    """存储难以查询的公众号对象"""

    __tablename__ = 'group_users'

    user_id = db.Column(db.Integer, nullable=False, default=0)
    app_id = db.Column(db.String(50), nullable=False, default='')
    wx_app_id = db.Column(db.Integer, nullable=False, default=0)
    openid = db.Column(db.String(50), nullable=False, default='')
    query_key = db.Column(db.Integer, nullable=False, default=0)


class Task(BaseModel):
    """推送任务"""
    
    __tablename__ = 'tasks'
    
    title = db.Column(db.String(20), nullable=False, default='')
    description = db.Column(db.String(255), nullable=False, default='')
    send_type = db.Column(db.String(30), nullable=False, default='')
    executed_at = db.Column(db.TIMESTAMP, default=None)
    

class TaskHasGroupMessage(BaseModel):
    """多公众号推送任务"""
    
    __tablename__ = 'task_has_group_messages'
    
    task_id = db.Column(db.Integer, nullable=False, default=0)
    group_msg_id = db.Column(db.Integer, nullable=False, default=0)


class SiloWxApp(BaseModel):
    """silo系统的公众号备份"""

    __tablename__ = 'silo_wx_apps'

    wx_app_id = db.Column(db.Integer, nullable=False, default=0)
    app_name = db.Column(db.String(255), nullable=False, default=0)
    description = db.Column(db.String(255), nullable=False, default=0)
    app_id = db.Column(db.String(255), nullable=False, default=0)
    origin_id = db.Column(db.String(30), nullable=False, default=0)
    alias = db.Column(db.String(50), nullable=False, default='')


class MassLog(BaseModel):
    """群发日志"""

    __tablename__ = 'mass_logs'

    task_has_group_msg_id = db.Column(db.Integer, nullable=False, default=0)
    wx_msg_id = db.Column(db.Integer, nullable=False, default=0)
    wx_msg_data_id = db.Column(db.Integer, nullable=False, default=0)
    state = db.Column(db.String(20), nullable=False, default='')
    total_count = db.Column(db.Integer, nullable=False, default=0)
    filter_count = db.Column(db.Integer, nullable=False, default=0)
    send_count = db.Column(db.Integer, nullable=False, default=0)
    error_count = db.Column(db.Integer, nullable=False, default=0)
